package com.zzyl.service.impl.handler;

import com.zzyl.entity.Resource;
import com.zzyl.utils.NoProcessing;
import com.zzyl.vo.TreeItemVo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 过滤一级菜单的链
 */
public class FirstResourceFilterHandler extends ResourceFilterHandler{
    private ResourceFilterHandler nextHandler;
    public void setNextHandler(ResourceFilterHandler nextHandler) {
        this.nextHandler = nextHandler;
    }
    /**
     * @param allList 所有的菜单数据
     * @param root    根节点
     *
     * root: children:[{},{}]
     */
    @Override
    public void filter(List<Resource> allList, TreeItemVo root, String parentResourceNo) {
        List<Resource> firstList = allList.stream().filter(r -> {
            if (NoProcessing.processString(r.getParentResourceNo()).equals(parentResourceNo)) {
                return true;
            }else{
                return false;
            }
        }).collect(Collectors.toList());
        //把一级目录转换为前端需要的vo格式
        /*List<TreeItemVo> fistTreeItemVo = firstList.stream().map(r -> {
            TreeItemVo v = new TreeItemVo();
            v.setId(r.getResourceNo());
            v.setLabel(r.getResourceName());
            return v;
        }).collect(Collectors.toList());*/

        List<TreeItemVo> fistTreeItemVo = parseItemVo(firstList);
        root.setChildren(fistTreeItemVo);
        if(null != nextHandler){
            nextHandler.filter(allList, root, parentResourceNo);
        }
    }
}
